#include<bits/stdc++.h>
using namespace std;
const int maxn = 11, maxm = 2005;
enum id {MP, ZP, FP};
const char *debugid[] = {"MP", "ZP", "FP"};
int n, m;
int nextCard;
char cardStack[maxm];
struct Pig {
    list<char> cards;
    id ID;
    bool equip_zgln;
    bool shown_id;
    bool act_as_good;
    bool act_as_bad_like;

    void playCard();
    void showInfo() {
        cout<<"ID: "<<debugid[ID]<<endl;
        cout<<"cards: ";
        for(auto it=cards.cbegin(); it!=cards.cend(); ++it) {
            cout<<*it<<' ';
        }
        cout<<endl;
    }
    void getCard() {
        for(int i=0; i<2; ++i) {
            cards.push_back(cardStack[nextCard]);
            if(nextCard!=m-1) nextCard++;
        }
    }
    void playCard() {
        for(auto it=cards.cbegin(), ed=cards.cend(); it!=ed; ++it) {
            char card = *it;
            if(canUse(card)) {
                switch(card) {

                }
            }
        }
    }
};
Pig pigs[maxn];
int turn;
struct Game {
public:
    int who;
    void readPlayer(int i) {
        string id; cin>>id;
        if(id=="MP") pigs[i].ID = MP;
        else if(id=="ZP") pigs[i].ID = ZP;
        else pigs[i].ID = FP;

        for(int j=0; j<4; ++j) {
            char card; cin>>card; pigs[i].cards.push_back(card);
        }
    }
    void readCards() {
        for(int i=0; i<m; ++i) {
            cin >> cardStack[i];
        }
    }
    void showGameInfo() {
        for(int i=0; i<n; ++i) {
            pigs[i].showInfo();
        }
        for(int i=nextCard; i<m; ++i) cout<<cardStack[i]<<' ';
        cout<<endl;
    }
};
int main() {
    cin >> n >> m;
    Game game;
    for(int i=0; i<n; ++i) {
        game.readPlayer(i);
    }
    game.readCards();
    // game.showGameInfo();
    // game.start();
    while(true) {
        if(game.ends()) break;
        pigs[turn].getCard();
        pigs[turn].playCard();
    }
    game.showGameInfo();
}
